class BookmarksController < ApplicationController
  before_filter :find_book

  # GET /book/1/bookmarks
  # GET /book/1/bookmarks.xml
  # GET /book/1/pages/1/bookmarks
  # GET /book/1/pages/1/bookmarks.xml
  def index
    conditions = params[:page_id] && ['page_number = ?', params[:page_id]]
    @bookmarks = @book.bookmarks.find(:all, :conditions=>conditions)
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @bookmarks }
    end
  end
  
  # GET /book/1/bookmarks/1
  # GET /book/1/bookmarks/1.xml
  def show
    @bookmark = @book.bookmarks.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @bookmark }
      format.jpg {send_file @bookmark.image_file_name, :type=>:jpg, :disposition=>'inline'}
    end
  end

  # GET /book/1/bookmarks/new
  # GET /book/1/bookmarks/new.xml
  def new
    @bookmark = @book.bookmarks.new(:page_number=>params[:page_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @bookmark }
    end
  end

  # GET /book/1/bookmarks/1/edit
  def edit
    @bookmark = @book.bookmarks.find(params[:id])
  end

  # POST /book/1/bookmarks
  # POST /book/1/bookmarks.xml
  def create
	  bounds = params[:bookmark].delete(:bounds)
    @bookmark = @book.bookmarks.new({'page_number'=>params[:page_id]}.merge params[:bookmark])
    @bookmark.bounds = [Float(bounds[:x]), Float(bounds[:y]), Float(bounds[:width]), Float(bounds[:height])] if bounds

    respond_to do |format|
      if @bookmark.save
        flash[:notice] = 'Bookmark was successfully created.'
        format.html { redirect_to(book_bookmark_url(@bookmark.book, @bookmark)) }
        format.xml  { render :xml => @bookmark, #:status => :created, # IE doesn't like this status
                        :location => @bookmark }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @bookmark.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /book/1/bookmarks/1
  # PUT /book/1/bookmarks/1.xml
  def update
	  bounds = params[:bookmark].delete(:bounds)
    @bookmark = @book.bookmarks.find(params[:id])

    respond_to do |format|
      if @bookmark.update_attributes(params[:bookmark])
        @bookmark.bounds = [Float(bounds[:x]), Float(bounds[:y]), Float(bounds[:width]), Float(bounds[:height])] if bounds
        @bookmark.save
		    
        flash[:notice] = 'Bookmark was successfully updated.'
        format.html { redirect_to(book_bookmark_path(@bookmark.book, @bookmark)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @bookmark.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /book/1/bookmarks/1
  # DELETE /book/1/bookmarks/1.xml
  def destroy
    @bookmark = @book.bookmarks.find(params[:id])
    @bookmark.destroy

    respond_to do |format|
      format.html { redirect_to(book_bookmarks_path(@bookmark.book)) }
      format.xml  { head :ok }
    end
  end
  
  protected
  def find_book
    @book = Book.find(params[:book_id])
  end
end
